<?php

declare(strict_types=1);

namespace App\Model;

use Hyperf\DbConnection\Model\Model;

/**
 *  今日缘分
 */
class GreetTodayLog extends Model
{
    protected $table = 'greet_today_log';
    protected $fillable = [
        "user_id", "host_id", "flag", "type", "cost_coin"
    ];

    //免费
    const TYPE_FREE = 0;
    //收费
    const TYPE_COST = 1;

    /**
     * 添加记录
     */
    static function addLog($userId = 0, $hostIds = [], $type = self::TYPE_FREE, $costCoin = 0.0)
    {
        if (!$userId || !$hostIds) return false;
        if (!is_array($hostIds)) $hostIds = [$hostIds];
        foreach ($hostIds as $hostId) {
            self::create([
                'user_id' => $userId,
                'host_id' => $hostId,
                'type' => $type,
                'cost_coin' => $costCoin,
            ]);
        }
        return true;
    }

    /**
     * 搭讪次数
     */
    static function checkCount($userId = 0, $offTime = '')
    {
        $where = [
            ['user_id', $userId],
        ];
        $offTime && $where[] = ['created_at', '>', $offTime];
        return self::where($where)->count();
    }
}
